import config from '../config/index'
import { getToken } from './storage'

type myMethod = "OPTIONS" | "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "PATCH" | "TRACE" | "CONNECT" | undefined
const baseURL = config.BaseUrl;
const prefix = '/api'
export default function (url: string, method: string, data = {}): Promise<PageResult<any[]>> | Promise<Result<any>> {
  //状态判定
  let meth: myMethod = method.toUpperCase() as myMethod
  if (meth != "GET" && meth != "DELETE" && meth != "POST" && meth != "PUT" && meth != "PATCH") {
    meth = "GET" // 不传情况下默认 'GET'
  }
  return new Promise(function (resolve, reject) {
    wx.request({
      url: baseURL + prefix + url,
      data: data,
      header: {
        Authorization: getToken()
      },
      //@ts-ignore
      method: meth,
      success: function (res) {
        //返回信息统一处理
        resolve(res.data as PageResult<any[]> | Result<any>);
      },
      fail: function (res) {
        //错误信息统一处理
        reject(res.errMsg);
      }
    })
  })
}
